import { Dropdown, Button, Menu, message, Modal } from 'antd';
import { DownOutlined } from '@ant-design/icons';
import styles from '@/pages/pc/lecturer/upload-courses/components/upload/index.less';
import { BjyVideoListModalUpload } from '@/pages/pc/components/Upload';
import { allGet } from '@/services/pc';
import AddForVideoLibrary from '@/pages/pc/lecturer/upload-courses/components/AddForVideoLibrary';
import React, { useState } from 'react';
import LinkModal from './link-modal';

export default ({ classid, handleUploadSuccess, onAddVideo, onImportVideo, children, cid }) => {
  const [visible, setVisible] = useState<boolean>(false);
  const onAddVideoWithUrl = async ({ url }: any) => {
    if (!url?.length) {
      return false;
    }
    const response = await allGet({
      params: {
        mod: 'lesson',
        do: 'add',
        version: 'pc1.0',
        classid,
        url,
        cid,
      },
    });
    if (response.success) {
      message.success('添加成功');
      handleUploadSuccess();
      setVisible(false);
    }
  };
  const auth = async () => {
    const response = await allGet({ params: { mod: 'polyv', do: 'uploadAuth', version: 'pc1.0' } });
    return { ...(response?.data || {}), ptime: response?.data?.ts };
  };
  const onClick = (a) => {
    if (a.key === '2') {
      setVisible(true);
    }
    if (a.key === '3') {
      onImportVideo();
    }
  };
  const menu = (
    <Menu onClick={onClick}>
      <Menu.Item key="1">
        <AddForVideoLibrary cid={cid} onSubmit={onAddVideo}>
          从视频库里添加
        </AddForVideoLibrary>
      </Menu.Item>
      <Menu.Item key="2">外部视频链接</Menu.Item>
      <Menu.Item key="3">
        <div className={styles.button}>
          <div className={styles.icon} />
          <div className={styles.text}>上传本地视频</div>
        </div>
      </Menu.Item>
    </Menu>
  );
  return [
    <Dropdown overlay={menu} placement="bottomLeft">
      {children || (
        <Button type="primary">
          导入视频 <DownOutlined />
        </Button>
      )}
    </Dropdown>,
    visible ? <LinkModal onClose={() => setVisible(false)} onSave={onAddVideoWithUrl} /> : '',
  ];
};
